import React, {Component} from 'react';
import { AliveScope } from 'react-activation';
import {BrowserRouter} from "react-router-dom";
import {renderRoutes} from "react-router-config";
import {routes} from "./Router";
import BaseComponent, {registerDoError} from "./redare/components/BaseComponent";
import {appApiInit} from "./AppApi";

function doError(error) {
    switch (error.code) {
        case 1000: //需要重新登录
            let backUrl = encodeURIComponent(window.location.href);
            window.location.href = window._ctxApi + '/login?backUrl=' + backUrl;
            break;
        default:
            break;
    }
}

registerDoError(doError);

export default class App extends BaseComponent {

    constructor(props) {
        super(props);
        appApiInit({key: `${(window._workbench || {})._apiKey}`});
    }

    render() {
        return (
            <BrowserRouter basename={this.getRouterBaseName()}>
                <AliveScope>
                {renderRoutes(routes)}
                </AliveScope>
            </BrowserRouter>
        )
    }
}
